// Logger.cpp: implementation of the CLogger class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <pgl/core/Logger.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace pgl
{

CLogger::CLogger()
: m_bEnabled(false)
{
	m_out<<_T("---------------------------------------------------------\n");
	m_out<<_T("-- Plot Graphic Library log - Starting\n");
	m_out<<_T("---------------------------------------------------------\n");
	m_out.flush();
}

CLogger::~CLogger()
{
	if (!m_bEnabled)
		return;

	m_out.flush();
	m_out<<_T("---------------------------------------------------------\n");
	m_out<<_T("-- Plot Graphic Library log - closed\n");
	m_out<<_T("---------------------------------------------------------\n");

	std::_tofstream out;
	out.open("pgllog.txt");
	if (out.is_open())
		out<<m_out.str();

	TRACE(_T("Logger closed\n"));
}

void CLogger::LogP( LPCSTR szFileName, UINT uLine, LPCTSTR szLog, va_list& list)
{
	if (!m_bEnabled)
		return;

	static TCHAR szBuffer[2048];
	USES_CONVERSION;

	// getting rid of full path...
	szFileName=strchr(szFileName,'\\')+1;

	_vstprintf( szBuffer, szLog, list);

	m_out.flush();
	m_out<<A2CT(szFileName)<<_T(" (")<<uLine<<_T(") ")<<szBuffer;
	m_out.flush();
}

void CLogger::LogP(LPCSTR szFileName, UINT uLine,  const std::_tostream& out)
{
	if( !IsEnabledP())
		return;

	USES_CONVERSION;

	// getting rid of full path...
	szFileName=strchr(szFileName,'\\')+1;

	m_out.flush();
	m_out<<A2CT(szFileName)<<_T(" (")<<uLine<<_T(") ")<<out;
	m_out.flush();
}

bool CLoggerSingleton::IsEnabled()
{
	return Instance()->IsEnabledP();
}

void CLoggerSingleton::SetEnabled(bool yes)
{
	Instance()->SetEnabledP(yes);
}

void CLoggerSingleton::Log( LPCSTR szFileName, UINT uLine, LPCTSTR szLog,...)
{
	va_list arguments;
	va_start(arguments, szLog);

	Instance()->LogP( szFileName, uLine, szLog, arguments);
	va_end(arguments);
}

void CLoggerSingleton::Log( LPCSTR szFileName, UINT uLine, const std::_tostream& out)
{
	Instance()->LogP(szFileName, uLine, out);
}


};
